package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.TransferAccountsZfbSafetyBatchAddDTO;
import org.jsola.hr.dto.TransferAccountsZfbSafetyBatchUpdateDTO;
import org.jsola.hr.query.TransferAccountsZfbSafetyBatchQuery;
import org.jsola.hr.service.ITransferAccountsZfbSafetyBatchService;
import org.jsola.hr.vo.TransferAccountsZfbSafetyBatchListVO;
import org.jsola.hr.vo.TransferAccountsZfbSafetyBatchVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 转账批次(支付宝安全发)
 *
 * @author zhr
 */
@RestController("hrTransferAccountsZfbSafetyBatchController")
@RequestMapping(HrConstants.API_PATH + "/v1/transferAccountsZfbSafetyBatches")
public class TransferAccountsZfbSafetyBatchController implements ITransferAccountsZfbSafetyBatchControllerAPI {

    @Autowired
    private ITransferAccountsZfbSafetyBatchService transferAccountsZfbSafetyBatchService;

    @Override
    @PostMapping
    public Result<TransferAccountsZfbSafetyBatchVO> save(@Valid @RequestBody TransferAccountsZfbSafetyBatchAddDTO transferAccountsZfbSafetyBatchAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增转账批次(支付宝安全发)权限校验

        return Result.success(
                transferAccountsZfbSafetyBatchService.save(transferAccountsZfbSafetyBatchAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody TransferAccountsZfbSafetyBatchUpdateDTO transferAccountsZfbSafetyBatchUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改转账批次(支付宝安全发)权限校验

        int count = transferAccountsZfbSafetyBatchService.update(transferAccountsZfbSafetyBatchUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{transferAccountsZfbSafetyBatchId}")
    public Result<Integer> delete(@PathVariable String transferAccountsZfbSafetyBatchId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除转账批次(支付宝安全发)权限校验

        int count = transferAccountsZfbSafetyBatchService.deleteByIds(tokenUser, transferAccountsZfbSafetyBatchId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] transferAccountsZfbSafetyBatchIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除转账批次(支付宝安全发)权限校验

        int count = transferAccountsZfbSafetyBatchService.deleteByIds(tokenUser, transferAccountsZfbSafetyBatchIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{transferAccountsZfbSafetyBatchId}")
    public Result<TransferAccountsZfbSafetyBatchVO> findById(@PathVariable String transferAccountsZfbSafetyBatchId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看转账批次(支付宝安全发)权限校验

        return Result.success(
            transferAccountsZfbSafetyBatchService.selectById(transferAccountsZfbSafetyBatchId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<TransferAccountsZfbSafetyBatchListVO>> listAll(@Valid TransferAccountsZfbSafetyBatchQuery transferAccountsZfbSafetyBatchQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看转账批次(支付宝安全发)权限校验

        return Result.success(
            transferAccountsZfbSafetyBatchService.select(transferAccountsZfbSafetyBatchQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<TransferAccountsZfbSafetyBatchListVO>> page(@Valid TransferAccountsZfbSafetyBatchQuery transferAccountsZfbSafetyBatchQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            transferAccountsZfbSafetyBatchService.selectPage(transferAccountsZfbSafetyBatchQuery, tokenUser.getSiteId())
        );
    }


}

